<?php
/**
 * project
 * 项目
 * @author Administrator
 * @version
 */
class Model_Tables_Project extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'project';
    /**
     * add
     * 增加项目
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'projectname' => $params['projectname'],
		'projectid' => $params['projectid'],
        'customername' => $params['customername'],
        'customercode' => $params['customercode'],	
		'startdate' => $params['startdate'],
		'enddate' => $params['enddate'],
        'createdate' => date("Y-m-d h:i:s",time()),
		'department' => $params['department'],
		'dutyperson' => $params['dutyperson'],
        'members' => ",".implode($params['members'],",").",",
        'goalcompound' => $params['goalcompound'],
        'ispublic' => $params['ispublic'],
        'workflow' => $params['workflow'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑项目
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'projectname' => $params['projectname'],
		'projectid' => $params['projectid'],
        'customername' => $params['customername'],
        'customercode' => $params['customercode'],
		'startdate' => $params['startdate'],
		'enddate' => $params['enddate'],
		'department' => $params['department'],
		'dutyperson' => $params['dutyperson'],
        //'members' => $params['members'],
        'members' => ",".implode($params['members'],",").",",
        'goalcompound' => $params['goalcompound'],
        'ispublic' => $params['ispublic'],
        'workflow' => $params['workflow'],
        );
        $where = 'id = '.$id ;
        //print_r($data);exit;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑项目
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转项目
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update project set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除项目
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取项目信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取项目数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($r = array())
    { 
    	$likefilter = array("projectname","projectid", "customername", "customercode");
    	$eqfilter =  array("department", "dutyperson", "createdate");
    	$privfilter = array("members");
        $select = $this->select()->from($this->_name,'count(*) as num');
        $where = " 1=1 ";
        $db = $this->getAdapter();
        foreach($r as $k=>$v){
        	if($k=="any"){
        		if($v){
        			$where .= $db->quoteInto(" and projectname like ? ","%$v%");
        			$where .= $db->quoteInto(" and projectid like ? ","%$v%");
        			$where .= $db->quoteInto(" and customername like ? ","%$v%");
        			$where .= $db->quoteInto(" and customercode like ? ","%$v%");
        		}
        	}else{
        		if(in_array($k,$likefilter)){
        			if($v)
        				$where .= $db->quoteInto(" and $k like ? ","%$v%");
        		}else if(in_array($k,$eqfilter)){
        			if($v)
        				$where .= $db->quoteInto(" and $k = ? ",$v);
        		}else if(in_array($k,$privfilter)){
        			if($v)
        				$where .= $db->quoteInto(" and ($k like ? ","%,$v,%").$db->quoteInto("  or dutyperson = ? or ispublic = 1) ",$v);
        		}
        	}
        }
        $select = $select->where($where);
    	/*if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("id=?", $s);
    		$select = $select->where($where);
    	}*/
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取项目数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$r = array())
    {
    	$likefilter = array("projectname","projectid", "customername", "customercode");
    	$eqfilter =  array("department", "dutyperson", "createdate");
    	$privfilter = array("members");
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        
        $where = " 1=1 ";
        $db = $this->getAdapter();
        foreach($r as $k=>$v){
        	if($k=="any"){
        		if($v){
        			$where .= $db->quoteInto(" and projectname like ? ","%$v%");
        			$where .= $db->quoteInto(" and projectid like ? ","%$v%");
        			$where .= $db->quoteInto(" and customername like ? ","%$v%");
        			$where .= $db->quoteInto(" and customercode like ? ","%$v%");
        		}
        	}else{
        		if(in_array($k,$likefilter)){
        			if($v)
        				$where .= $db->quoteInto(" and $k like ? ","%$v%");
        		}else if(in_array($k,$eqfilter)){
        			if($v)
        				$where .= $db->quoteInto(" and $k = ? ",$v);
        		}else if(in_array($k,$privfilter)){
        			if($v)
        				$where .= $db->quoteInto(" and ($k like ? ","%,$v,%").$db->quoteInto("  or dutyperson = ? or ispublic = 1) ",$v);
        		}
        	}
        }

        /*if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("id= ?", $s);
		} */
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    public function getProjectList(){
    	$result = $this->getList(1,1000);
    	$projects = array();
        if (!empty($result))
	        {
	            foreach ($result as $item)
	            {
	                $projects[$item['id']] = $item['projectid'];
	            }
	        }
        return $projects;
    }
    public function getProjectListByMember($uid){
    	$db = $this->getAdapter();
    	$sql = $db->quoteInto("select id,projectid from project where members like ? or ispublic =1","%,$uid,%");
    	$result = $db->fetchAll($sql);
    	$projects = array();
    	$projects["-1"]="请选择";
    	if (!empty($result))
    	{
    		foreach ($result as $item)
    		{
    			$projects[$item['id']] = $item['projectid'];
    		}
    	}
    	return $projects;
    }
    public function getCompoundNum($projectid){
    	$db = $this->getAdapter();
    	$sql = "select count(*) from compound where projectid = :projectid";
    	$result = $db->fetchOne($sql,array("projectid"=>$projectid));
    	return $result;
    }
}